import tornado.web
import json
from src.ucloud.ufile_log_list import read_log_list_page


class ApiListHandler(tornado.web.RequestHandler):

    def get(self, *args, **kwargs):
        pass

    def post(self, *args, **kwargs):
        page_last_file = ''
        page_size = 30
        prefix = ''

        json_data = json.loads(self.request.body)
        if json_data is not None:
            parms = json_data['parms']
            if parms is not None:
                page_last_file = parms['page_last_file']
                page_size = parms['page_size']
                prefix = parms['prefix']
            else:
                resp = {'code': 500, 'msg': "parms error"}
                self.write(json.dumps(resp, ensure_ascii=False))
        else:
            resp = {'code': 500, 'msg': "parms error"}
            self.write(json.dumps(resp, ensure_ascii=False))
        log_list, is_next = read_log_list_page(page_last_file, page_size, prefix)
        resp = {'code': 0, 'data': log_list, 'next': is_next}
        self.write(json.dumps(resp, ensure_ascii=False))
